package com.liesheng.haylou.ui.setting;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.liesheng.haylou.R;
import com.liesheng.haylou.base.BaseActivity;
import com.liesheng.haylou.base.BaseVm;
import com.liesheng.haylou.bluetooth.BleManager;
import com.liesheng.haylou.databinding.ActivitySettingBinding;
import com.liesheng.haylou.db.DBManager;
import com.liesheng.haylou.event.DelDeviceEvent;
import com.liesheng.haylou.ui.main.MainActivity;
import com.liesheng.haylou.view.dialog.ConfirmDialog;

import org.greenrobot.eventbus.EventBus;

/**
 * Date:${DATA}
 * Author ${ZhangZhiQiang}
 * Description
 */
public class SettingActivity extends BaseActivity<ActivitySettingBinding, BaseVm> {

    String address;

    @Override
    protected ActivitySettingBinding getmBinding() {
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_setting, null, false);
        return mBinding;
    }

    @Override
    protected BaseVm getViewModel() {
        return null;
    }

    @Override
    protected void initData() {
        setTitle("设置");
        showHeadLayout(true);
        setStatusBarColor(R.color.main_bg, false);

        String version = getIntent().getStringExtra("version");
        address = getIntent().getStringExtra("address");

        mBinding.tvVersion.setText(TextUtils.isEmpty(version) ? "" : version);
    }

    public static void startBy(BaseActivity activity, String address, String version) {
        Intent intent = new Intent(activity, SettingActivity.class);
        intent.putExtra("version", version);
        intent.putExtra("address", address);
        activity.startActivity(intent);
    }

    @Override
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.rlHelpName:
                HelpActivity.startBy(this);
                break;
            case R.id.rlLegalmsg:
                MsLegalActivity.startBy(this);
                break;
            case R.id.rlHelp1:
                HelpPageActivity.startBy(this);
                break;
            case R.id.btUnbind:

                ConfirmDialog.newInstance().setMessage(R.string.dialog_unbind_device_msg)
                        .setSubmitOnclickListener(new ConfirmDialog.SubmitOnclickListener() {
                            @Override
                            public void onSubmit() {
                                DBManager.getInstance().getDaoSession().getBoundedDeviceDao().deleteByKey(address);
                                EventBus.getDefault().post(new DelDeviceEvent(address));
                                BleManager.getInstance().closeConn(address);
                                MainActivity.startBy(SettingActivity.this);
                                finish();
                            }
                        }).show(getSupportFragmentManager());
                break;
        }
    }
}
